class Solution {
public:
    vector<int> f;
    int Find(int x){
        
        if(x!=f[x]){
            f[x]=Find(f[x]);
        }

        return f[x];
    }
    void Union(int x,int y,auto& has){

        x=Find(x),y=Find(y);
        if(x==y){
            return;
        }
        if(has[x].size()>has[y].size()){
            swap(x,y);
        }
        has[y].insert(has[x].begin(),has[x].end());
        has[x].clear();
        f[x]=y;
    }
    vector<int> processQueries(int c, vector<vector<int>>& connections, vector<vector<int>>& queries) {
        
        vector<int> vecRet;
        f.resize(c+1);
        vector<set<int>> has(c+1);
        for(int i=1;i<=c;++i){
            f[i]=i;
            has[i].insert(i);
        }
        
        for(int i=0;i<connections.size();++i){
            int x=connections[i][0],y=connections[i][1];
            Union(x,y,has);
        }
        for(int i=0;i<queries.size();++i){
            int op=queries[i][0],x=queries[i][1];
            if(op==1){
                int fa=Find(x);
                if(has[fa].size()==0){
                    vecRet.push_back(-1);
                }
                else{
                    if(has[fa].count(x)){
                        vecRet.push_back(x);
                    }
                    else{
                        vecRet.push_back(*has[fa].begin());
                    }
                }
            }
            else{
                int fa=Find(x);
                has[fa].erase(x);
            }   
        }

        return vecRet;
    }
};
